package com.sxg.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sxg.entity.SxgOrderEntity;
import com.sxg.enums.OrderStatusEnum;
import com.sxg.service.SxgOrderService;
import com.sxg.vo.CloseOrder;
import com.sxg.vo.DeliveryOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
@RabbitListener(queues = "order.queue")
public class OrderDelayReceiveListener {

    @Resource
    private SxgOrderService sxgOrderService;

    /**
     * 订单超时关单
     *
     * @param closeOrder 接收的关单消息
     */
    @RabbitHandler
    public void closeOrder(@Payload CloseOrder closeOrder) {
        log.error("订单超时关单，订单号：{}", closeOrder.getOrderId());
        sxgOrderService.close(closeOrder.getOrderId());
    }

    /**
     * 订单发货消息
     *
     * @param deliveryOrder 接收的发货消息
     */
    @RabbitHandler()
    public void deliveryOrder(@Payload DeliveryOrder deliveryOrder) {
        log.error("订单发货，订单号：{}", deliveryOrder.getOrderId());
        sxgOrderService.update(Wrappers.<SxgOrderEntity>lambdaUpdate()
                .set(SxgOrderEntity::getStatus, OrderStatusEnum.PAYED.getCode())
                .eq(SxgOrderEntity::getOrderId, deliveryOrder.getOrderId()));
    }

}
